﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using HelpCentral.Core.Services;
using HelpCentral.Core.Objects;

namespace HelpCentral.ClientManager.Web
{
    /// <summary>
    /// Summary description for ClientManagement
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class ClientManagement : System.Web.Services.WebService
    {
        [WebMethod]
        public string SaveClient(int id, string name, Guid license, DateTime activateDate, DateTime? deactivateDate)
        {
            var cs = new ClientService();

            // check if client already exists
            if (id == -1)
            {
                var c = cs.GetClients(false).FirstOrDefault(x => x.Name == name || x.License == license);

                if (c != null)
                {
                    if (c.Name == name)
                    {
                        return "Client '" + name + "' already exists.";
                    }
                    else if (c.License == license)
                    {
                        return "Client with license '" + license.ToString() + "' already exists.";
                    }
                }
            }

            var ci = new ClientInfo();

            ci.ClientId = id;
            ci.Name = name;
            ci.License = license;
            ci.ActivateDate = activateDate;
            ci.DeactivateDate = deactivateDate;

            cs.SaveClient(ci);

            return "Success";
        }
    }
}
